﻿using Microsoft.AspNetCore.Mvc;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class DictController : Controller
	{
		DictRepository dictRepository = new DictRepository();


		/// <summary>
		/// 获取所有字典类型
		/// </summary>
		/// <returns></returns>
		[HttpGet("GetTypes")]
		public IActionResult GetTypes()
		{
			List<Dict_type> dict_Types = dictRepository.GetDict_types();
			return Ok(dict_Types);
		}


		/// <summary>
		/// 根据str查询字典类型
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetType")]
		public IActionResult GetType(string str)
		{
			List<Dict_type> dict_Type = dictRepository.GetDict_type(str);
			return Ok(dict_Type);
		}


		/// <summary>
		/// 根据字典类型的id获取一种字典
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet("GetOneDict")]
		public IActionResult GetOneDict(int id)
		{
			List<Dict> dict = dictRepository.GetOneDict(id);
			return Ok(dict);
		}


		/// <summary>
		/// 查询字典
		/// </summary>
		/// <param name="type"></param>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetDict")]
		public IActionResult GetDict(int page, int pageSize, int type, int state, string? str)
		{
			Dict_FY dictionaries = dictRepository.GetDict(page, pageSize, type, state, str);
			return Ok(dictionaries);
		}

		
		[HttpGet("GetDict_Codes")]
		public IActionResult GetDict_Codes(int type,string? codes)
		{
			List<Dictionary> dictionaries = dictRepository.GetDict_Codes(type,codes);
			return Ok(dictionaries);
		}


		/// <summary>
		/// 状态改变
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = dictRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增字典
		/// </summary>
		/// <param name="dict"></param>
		/// <returns></returns>
		[HttpPost("AddDict")]
		public IActionResult AddDict(Dictionary dict)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(dict.dictionary_code)
				|| string.IsNullOrEmpty(dict.dictionary_name)
				|| dict.dictionary_type <= 0)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = dictRepository.AddDict(dict);
			return Ok(res);
		}


		/// <summary>
		/// 修改字典
		/// </summary>
		/// <param name="dict"></param>
		/// <returns></returns>
		[HttpPut("UpdateDict")]
		public IActionResult UpdateDict(Dictionary dict)
		{
			// 数据验证（可选）
			if (dict.dictionary_id <= 0
				|| string.IsNullOrEmpty(dict.dictionary_code)
				|| string.IsNullOrEmpty(dict.dictionary_name)
				|| dict.dictionary_type <= 0)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = dictRepository.UpdateDict(dict);
			return Ok(res);
		}
        [HttpGet("GetName")]
        public IActionResult GetName(int dictionary_id)
        {
            string dict = dictRepository.GetName(dictionary_id);
            return Ok(dict);
        }
        


		/// <summary>
		/// 删除字典
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelDict")]
		public IActionResult DelDict(int id)
		{
			bool res = dictRepository.DelDict(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除字典
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelDicts")]
		public IActionResult DelDicts(int[] ids)
		{
			bool res = dictRepository.DelDicts(ids);
			return Ok(res);
		}
	}

}

